package me.like.diandian.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import androidx.core.app.ActivityCompat;

import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import me.like.diandian.R;
import me.like.diandian.base.BaseActivity;
import me.like.diandian.databinding.ActivityWebviewBinding;

/**
 * Created by shousakai on 2019/6/22.
 */

public class ExploreActivity extends BaseActivity<ActivityWebviewBinding> {
    private String mWebUrl = null;
    private String mWebTitle = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWebViewSettings();
        binding.webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(com.tencent.smtt.sdk.WebView webView, int i) {
                super.onProgressChanged(webView, i);
                if (i == 100) {
                    binding.progressBar1.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    binding.progressBar1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    binding.progressBar1.setProgress(i);//设置进度值
                }
            }
        });
        //如果不设置WebViewClient，请求会跳转系统浏览器
        binding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if(url.endsWith("apk")){
                    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
                    startActivity(intent);
                }else {
                    view.loadUrl(url);
                }

                return true;
            }
        });
        mWebTitle = getIntent().getStringExtra("mWebTitle");
        mWebUrl = getIntent().getStringExtra("mWebUrl");
        binding.tlvTitle.setTitle(mWebTitle);
        binding.tlvTitle.setBackgroundColor(ActivityCompat.getColor(this, R.color.white));
        binding.tlvTitle.setLeftimgtype(2);
        binding.webView.loadUrl(mWebUrl);
        binding.refresh.setEnableLoadmore(false);
        binding.refresh.setOnRefreshListener((efreshlayout) -> {
            binding.webView.loadUrl(mWebUrl);
            binding.refresh.finishRefresh();
        });
        setstateBar(true);
    }

    public static void startActivity(Context context, String mWebUrl, String mWebTitle) {
        Intent i = new Intent(context, ExploreActivity.class);
        i.putExtra("mWebUrl", mWebUrl);
        i.putExtra("mWebTitle", mWebTitle);
        context.startActivity(i);
    }

    private void initWebViewSettings() {
        WebSettings webSetting = binding.webView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
        // settings 的设计
    }

}
